package com.ninogame.state;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Currency;

import org.xmlpull.v1.XmlPullParserException;

import com.ninogame.ArrayMap;
import com.ninogame.Dialog;
import com.ninogame.GameLayer;
import com.ninogame.GameLib;
import com.ninogame.IConstant;
import com.ninogame.MazeEscape;
import com.ninogame.Map;
import com.ninogame.Sprite;
import com.ninogame.Actor.Creature;
import com.ninogame.Actor.Explorer;
import com.ninogame.resolution.DEF;


import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint.Style;
import android.util.Log;


public class StateGameplay extends MazeEscape
{

	public static Map map;
	public static Explorer explorer;
	public static ArrayList<Creature> creature;
	public static int m_CurrentLevel = 1; //begin is 1
	public static int s_UnlockLevel = 11; //begin is 1
	static GameLayer gameLayer;
	static Bitmap boardBuffer;	
	static Canvas canvasBoardBuffer;
	
	public static Sprite spriteDPad = null;
	public static Sprite spriteWall = null;
	public static boolean isIngame = false;

	public static synchronized void SendMessage(int type)
	{

		switch (type)
		{
		case MESSAGE_CTOR:
			isIngame = true;
			if(boardBuffer == null)
			boardBuffer = Bitmap.createBitmap(Map.MAP_CELL_WIDTH * Map.MAP_COLUMNS, Map.MAP_CELL_WIDTH * Map.MAP_ROWS, Config.ARGB_4444);
			canvasBoardBuffer = new Canvas(boardBuffer);			
			explorer = new Explorer(-1, -1, 20, 20); // postion will create in create map
			creature = new ArrayList<Creature>();
			setDpadPosition();
			if(gameLayer != null)
			{
				gameLayer.resetAll();
				gameLayer = null;
			}
			gameLayer = new GameLayer();
			map = new Map(m_CurrentLevel); 
			explorer.sprite.setAnim(0, explorer.m_x, explorer.m_y, true, false);

			try {
				GameLayer.loadLayerBG("map/level1.tmx");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//clean wall in map
			//GameLayer.arrayListDataMap.get(0)[6][6] = -1;
			//GameLayer.arrayListDataMap.get(0)[7][6] = -1;

			//Log.d("map.doorExit_Row"," " + map.doorExit_Row);
			//Log.d("map.doorExit_Col"," " + map.doorExit_Col);
			int ALIGN_DOOR_COLUMN = 1;
			int ALIGN_DOOR_ROW = 1;
			if (map.doortype == ArrayMap.TYPE_EXIT_TOP) { // = 1
				GameLayer.arrayListDataMap.get(1)[map.doorExit_Row][ALIGN_DOOR_COLUMN + map.doorExit_Col * 2] = -1;
				GameLayer.arrayListDataMap.get(1)[map.doorExit_Row][ALIGN_DOOR_COLUMN + map.doorExit_Col * 2 + 1] = -1;
			}
			if (map.doortype == ArrayMap.TYPE_EXIT_BOTTOM) { // = 2
				GameLayer.arrayListDataMap.get(1)[(map.doorExit_Row + ALIGN_DOOR_ROW) * 2 + 1][ALIGN_DOOR_COLUMN + map.doorExit_Col * 2] = -1;
				GameLayer.arrayListDataMap.get(1)[(map.doorExit_Row + ALIGN_DOOR_ROW) * 2 + 1][ALIGN_DOOR_COLUMN + map.doorExit_Col * 2 + 1] = -1;
			}
			if (map.doortype == ArrayMap.TYPE_EXIT_LEFT) { // = 4
				GameLayer.arrayListDataMap.get(1)[(map.doorExit_Row) * 2 + ALIGN_DOOR_ROW][ALIGN_DOOR_COLUMN -1 + (map.doorExit_Col) * 2] = -1;
				GameLayer.arrayListDataMap.get(1)[(map.doorExit_Row) * 2 + ALIGN_DOOR_ROW +1][ALIGN_DOOR_COLUMN -1 + (map.doorExit_Col) * 2] = -1;
			}
			if (map.doortype == ArrayMap.TYPE_EXIT_RIGHT) { // = 8
				GameLayer.arrayListDataMap.get(1)[(map.doorExit_Row) * 2 + ALIGN_DOOR_ROW][ALIGN_DOOR_COLUMN + (map.doorExit_Col + 1) * 2] = -1;
				GameLayer.arrayListDataMap.get(1)[(map.doorExit_Row) * 2 + ALIGN_DOOR_ROW+1][ALIGN_DOOR_COLUMN + (map.doorExit_Col + 1) * 2] = -1;
			}
			break;
		case MESSAGE_UPDATE:

			if (Dialog.isShowDialog)
				return;
			updatetouch();
			explorer.update();

			for (int i = 0; i < creature.size(); i++)
				creature.get(i).update();

			if (explorer.mstate == Explorer.STATE_WIN) {
				changeState(STATE_WIN,true,false);
				creature.clear();
				explorer = new Explorer(-1, -1, 20, 20);
				m_CurrentLevel++;
			//	map = new Map(s_CurrentLevel);
			} else if (explorer.mstate == Explorer.STATE_DIE) {
				changeState(STATE_LOSE);
			}
			break;

		case MESSAGE_PAINT:
			// Log.d("draw 1","draw 1");

			GameLayer.drawLayerBackGround(canvasScreenBuffer);
			//	mainPaint.setStyle(Style.FILL);
			//	mainPaint.setARGB(150, 0, 0, 0);
			//	canvasScreenBuffer.drawRect(0, 0, 300, SCREEN_HEIGHT, mainPaint);
			boardBuffer.eraseColor(Color.TRANSPARENT);
			map.draw(canvasBoardBuffer);
			//explorer.draw(canvasBoardBuffer);
			//for (int i = 0; i < creature.size(); i++)
			//	creature.get(i).draw(canvasBoardBuffer);
			//canvasScreenBuffer
			canvasScreenBuffer.drawBitmap(boardBuffer, 100, 130, mainPaint);
			MazeEscape.matrix.reset();
			MazeEscape.matrix.postScale(SCREEN_WIDTH / 800f, SCREEN_HEIGHT / 1280f);//dua vap file map
			//matrix.postScale(1, 1);
			matrix.postTranslate(0, 0); // move it into x, y position
			//MainActivity.matrix.postTranslate(0,0);		
			mainCanvas.drawBitmap(bitmapScreenBuffer, MazeEscape.matrix, mainPaint);
			//	mainCanvas.drawBitmap(boardBuffer, 350, 40, mainPaint);
			
				drawHUD(mainCanvas);
			break;

		case MESSAGE_DTOR:
			isIngame = false;
			break;
		}
	}

	public static boolean isCreatureMoving()
	{
		int size = creature.size();
		int i = 0;
		while (i < size) {
			if (creature.get(i).ismove == true)
				return true;
			i++;
		}

		return false;
	}

	//
	public static void drawHUD(Canvas c)
	{
		if (Dialog.isShowDialog || mCurrentState != STATE_GAMEPLAY)
			return;
		//draw dpad
		spriteDPad.drawAFrame(c, 0, DEF.DPAD_BEGIN_X, DEF.DPAD_BEGIN_Y);
		if (explorer.mstate == Explorer.STATE_IDLE) {
			if (isTouchDrapInRect(DEF.DPAD_RIGHT_X, DEF.DPAD_RIGHT_Y, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT)) {
				spriteDPad.drawAFrame(c, DEF.FRAME_DPAD_RIGHT, DEF.DPAD_BEGIN_X, DEF.DPAD_BEGIN_Y);
			} else if (isTouchDrapInRect(DEF.DPAD_LEFT_X, DEF.DPAD_LEFT_Y, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT)) {
				spriteDPad.drawAFrame(c,  DEF.FRAME_DPAD_LEFT, DEF.DPAD_BEGIN_X, DEF.DPAD_BEGIN_Y);
			} else if (isTouchDrapInRect(DEF.DPAD_BOTTOM_X, DEF.DPAD_BOTTOM_Y, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT)) {
				spriteDPad.drawAFrame(c,  DEF.FRAME_DPAD_BOTTOM, DEF.DPAD_BEGIN_X, DEF.DPAD_BEGIN_Y);
			} else if (isTouchDrapInRect(DEF.DPAD_TOP_X, DEF.DPAD_TOP_Y, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT)) {
				spriteDPad.drawAFrame(c,  DEF.FRAME_DPAD_TOP, DEF.DPAD_BEGIN_X, DEF.DPAD_BEGIN_Y);

			} else if (isTouchDrapInRect(DEF.DPAD_TOP_X, DEF.DPAD_TOP_Y + DEF.DPAD_HEIGHT, DEF.DPAD_WIDTH, DEF.DPAD_HEIGHT)) {
				spriteDPad.drawAFrame(c,  DEF.FRAME_DPAD_CENTER, DEF.DPAD_BEGIN_X, DEF.DPAD_BEGIN_Y);
			}
		}
		//draw pause button
		if (isTouchDrapInRect(DEF.PAUSE_X - DEF.PAUSE_W / 2, DEF.PAUSE_Y - DEF.PAUSE_W / 2, DEF.PAUSE_W, DEF.PAUSE_H))
			spriteDPad.drawAFrame(MazeEscape.mainCanvas, DEF.FRAME_PAUSE_HIGHTLIGHT, DEF.PAUSE_X, DEF.PAUSE_Y);
		else
			spriteDPad.drawAFrame(MazeEscape.mainCanvas, DEF.FRAME_PAUSE_NORMAL, DEF.PAUSE_X, DEF.PAUSE_Y);
	}

	public static void updatetouch()
	{
		if (isTouchReleaseInRect(DEF.PAUSE_X - DEF.PAUSE_W / 2, DEF.PAUSE_Y - DEF.PAUSE_W / 2, DEF.PAUSE_W, DEF.PAUSE_H)) {
			changeState(STATE_IGM,true,false);
		}

	}
	//set position of dpad
	public static void setDpadPosition()
	{	
		DEF.DPAD_WIDTH = spriteDPad.getFrameWidth(DEF.FRAME_DPAD_CENTER);
		DEF.DPAD_HEIGHT = spriteDPad.getFrameHeight(DEF.FRAME_DPAD_CENTER);
		 
		DEF.PAUSE_W = spriteDPad.getFrameWidth(DEF.FRAME_DPAD_CENTER);
		DEF.PAUSE_H = spriteDPad.getFrameHeight(DEF.FRAME_DPAD_CENTER);
		
		float scalex= SCREEN_WIDTH*1.0f/800;
		float scaley= SCREEN_HEIGHT*1.0f/1280;
		DEF.PAUSE_X =(int)(DEF.PAUSE_X*scalex);
		DEF.PAUSE_Y =(int)(DEF.PAUSE_Y*scaley);
				
		DEF.DPAD_BEGIN_X = (int)(40*scalex);
		DEF.DPAD_BEGIN_Y = (int)((1280 - 450)*scaley);
		
		DEF.DPAD_CENTER_X = DEF.DPAD_BEGIN_X + spriteDPad.getFrameBeginX(DEF.FRAME_DPAD_CENTER); 
		DEF.DPAD_CENTER_Y = DEF.DPAD_BEGIN_Y + spriteDPad.getFrameBeginY(DEF.FRAME_DPAD_CENTER);
		
		DEF.DPAD_TOP_X = DEF.DPAD_BEGIN_X + spriteDPad.getFrameBeginX(DEF.FRAME_DPAD_TOP); 
		DEF.DPAD_TOP_Y = DEF.DPAD_BEGIN_Y + spriteDPad.getFrameBeginY(DEF.FRAME_DPAD_TOP);
		
		DEF.DPAD_RIGHT_X = DEF.DPAD_BEGIN_X + spriteDPad.getFrameBeginX(DEF.FRAME_DPAD_RIGHT); 
		DEF.DPAD_RIGHT_Y = DEF.DPAD_BEGIN_Y + spriteDPad.getFrameBeginY(DEF.FRAME_DPAD_RIGHT);
		
		DEF.DPAD_BOTTOM_X = DEF.DPAD_BEGIN_X + spriteDPad.getFrameBeginX(DEF.FRAME_DPAD_BOTTOM); 
		DEF.DPAD_BOTTOM_Y = DEF.DPAD_BEGIN_Y + spriteDPad.getFrameBeginY(DEF.FRAME_DPAD_BOTTOM);
		
		DEF.DPAD_LEFT_X = DEF.DPAD_BEGIN_X + spriteDPad.getFrameBeginX(DEF.FRAME_DPAD_LEFT); 
		DEF.DPAD_LEFT_Y = DEF.DPAD_BEGIN_Y + spriteDPad.getFrameBeginY(DEF.FRAME_DPAD_LEFT);
		

	}

}
